package com.btkjgc.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AccountInfo;
import com.btkjgc.models.AccountUser;
import com.btkjgc.models.AdminUser;
import com.btkjgc.models.CarAccount;
import com.btkjgc.models.Dictionary;
import com.btkjgc.models.extend.AccountUserModel;
import com.btkjgc.service.intf.AccountUserGeneralService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Service
public class AccountUserGeneralService_Impl extends BaseService_Impl implements AccountUserGeneralService_Intf{
	
	static Logger logger = Logger.getLogger(AccountUserGeneralService_Impl.class);
	
	@Override
	public void setListData(HttpServletRequest request, ModelAndView mv,int pageIndex, int totalRecord, 
			List<AccountUserModel> accountUserModel) {
		// TODO Auto-generated method stub
		
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements())
		{
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}
		
		mv.addObject("account_users", accountUserModel);
		
		//账户类型列
		String typesql =" SELECT dt.`key`,dt.`value` FROM dictionary_t dt WHERE dt.`name`='AccType'";
		
		List<Dictionary> typeList = HibernateUtil.executeSQLQueryList(typesql,Dictionary.class);
		if(typeList.size() > 0)
		{
			mv.addObject("typeList", typeList);
		}
		
		//公司列
		String companysql = " select aut.name,CONCAT(id,'') 'key' from account_user_t aut where aut.type=1 and aut.del = 0";
		
		List<Dictionary> companyList = HibernateUtil.executeSQLQueryList(companysql,Dictionary.class);
		if (companyList.size() > 0)
		{
			mv.addObject("companyList", companyList);
		}
		
		//车型列
		String cartypesql = " select ct.name,CONCAT(id,'') 'key' from cartype_t ct where ct.del = 0";
		
		List<Dictionary> cartypeList = HibernateUtil.executeSQLQueryList(cartypesql,Dictionary.class);
		if(cartypeList.size() > 0)
		{
			mv.addObject("cartypeList", cartypeList);
		}
		
		//城市列
		String citysql = " select DISTINCT(ct.Name),ct.id 'key' from cities_t ct LEFT JOIN account_user_t aut ON aut.cityid=ct.id where aut.del=0";
		
		List<Dictionary> cityList = HibernateUtil.executeSQLQueryList(citysql,Dictionary.class);
		if(cityList.size() > 0)
		{
			mv.addObject("cityList", cityList);
		}
		
		//车辆状态列
		String statussql = " SELECT dt.`key`,dt.`value` FROM dictionary_t dt WHERE dt.`name`='CarStatus'";
		
		List<Dictionary> statusList = HibernateUtil.executeSQLQueryList(statussql, Dictionary.class);
		if(statusList.size() > 0)
		{
			mv.addObject("statusList", statusList);
		}
		
		mv.addObject("currentpage", pageIndex);
		mv.addObject("totalRecord", totalRecord);
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Integer insertAccountUser(AccountUserModel accountUserModel) {
		// TODO Auto-generated method stub
		AccountUser accountUser = new AccountUser();
		String d=new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
		
		try{
			PropertyUtils.copyProperties(accountUser, accountUserModel);
			//设置操作员、操作时间
			AdminUser loginUser = getLoginUser();
			accountUser.setOperatorid(loginUser.getId());
			accountUser.setOperdate(new Date());
			Integer id=HibernateUtil.save(accountUser);
			//如果是个人用户，插入个人车辆表
			if(accountUserModel.getType()==0){
				CarAccount carAccount = new CarAccount();
				carAccount.setAccountid(id);
				carAccount.setDel(1);
				carAccount.setCreatedate(new Date());
				HibernateUtil.save(carAccount);
			}
			//初始account_info_t表的数据
			insertAccountInfo(id);
			//设置account
			int length = id.toString().length();
			String account = "";
			account = account+d;
			for(int i=0;i<8-length;i++){
				account+="0";
			}
			account=account+id;
			String hql = " update from AccountUser set account='"+account+"' where id="+id;
			HibernateUtil.executeUpdate(hql);
			
			return 1;
		}catch(Exception e){
			return -11;
		}
	}
	
	@Override
	public void insertAccountInfo(Integer mid) {
		// 向account_info_t表初始化数据
		AccountInfo accountInfo = new AccountInfo();
		accountInfo.setAccountid(mid);
		accountInfo.setAvailable_balance(0);
		accountInfo.setTotal_back_balance(0);
		accountInfo.setTotal_consum(0);
		accountInfo.setTotal_recharge(0);
		accountInfo.setTotal_refund(0);
		accountInfo.setCoupon_num(0);
		accountInfo.setTotal_score(0);
		accountInfo.setTotal_free_trace(0);
		
		HibernateUtil.save(accountInfo);
		
	}
	
}